// pages/search/searchIndex/searchIndex.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        latestSearch: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        if (wx.getStorageSync('latestSearch')) {
            this.setData({
                latestSearch: wx.getStorageSync('latestSearch')
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    goSearchResult: function(e) {
        let latestSearch = this.data.latestSearch
        if (latestSearch.indexOf(e.detail.value) == -1) {
            latestSearch.unshift(e.detail.value)
            wx.setStorageSync('latestSearch', latestSearch)
        }
        wx.navigateTo({
            url: '/pages/search/searchList/searchLIst?keyWord=' + e.detail.value
        })
    },

    latestClick: function(e) {
        wx.navigateTo({
            url: '/pages/search/searchList/searchLIst?keyWord=' + e.currentTarget.dataset.key
        })
    },

    goDelete: function() {
        const that = this
        wx.showModal({
            title: '提示',
            content: '确认删除搜索记录？',
            success(res) {
                if (res.confirm) {
                    wx.removeStorageSync('latestSearch')
                    that.setData({
                        latestSearch: []
                    })
                } else if (res.cancel) {}
            }
        })
    }
})